
var provice = [{
	name: "重庆",
	city: [{
		name: "重庆市",
		districtAndCounty: ["重庆市","渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "万盛区", "渝北区", "巴南区", "万州区", "涪陵区", "黔江区", "长寿区", "合川区", "永川区", "江津区", "南川区", "綦江县", "潼南县", "铜梁区", "大足区", "荣昌区", "璧山区", "垫江县", "武隆县", "丰都县", "城口县", "梁平区", "开州区", "巫溪县", "巫山县", "奉节县", "云阳县", "忠县", "石柱土家族自治县", "彭水苗族土家族自治县", "酉阳土家族苗族自治县", "秀山土家族苗族自治县"],
		districtAndCountyCode: ["500000","500103", "500104", "500105", "500106", "500107", "500108", "500109", "507509", "500112", "500113", "500101", "500102", "500114", "500115", "500117", "500118", "500116", "500119", "500110", "500223", "500224", "500111", "500226", "500227", "500231", "500232", "500230", "500229", "500228", "500234", "500238", "500237", "500236", "500235", "500233", "500240", "500243", "500242", "500241"],		
		//districtAndCounty: ["重庆市","渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "万盛区", "渝北区", "巴南区", "万州区", "涪陵区", "黔江区", "长寿区", "合川区", "永川区", "江津区", "南川区", "綦江县", "潼南县", "铜梁区", "大足区", "荣昌区", "璧山区", "垫江县", "武隆县", "丰都县", "城口县", "梁平区", "开州区", "巫溪县", "巫山县", "奉节县", "云阳县", "忠县", "石柱土家族自治县", "彭水苗族土家族自治县", "酉阳土家族苗族自治县", "秀山土家族苗族自治县"],
	   //districtAndCountyCode: ["50", "5003", "5004", "5005", "5006", "5007", "5008", "5009", "507509", "500112", "500113", "500101", "500102", "500114", "500115", "500117", "500118", "500116", "500119", "500110", "500223", "500224", "500111", "500226", "500227", "500231", "500232", "500230", "500229", "500228", "500234", "500238", "500237", "500236", "500235", "500233", "500240", "500243", "500242", "500241"],
		districtAndCountyLat: ["29.95609", "29.561946", "29.49089", "29.587213", "29.547200", "29.509813", "29.530511", "29.809618", "28.961261", "29.694089", "29.406832", "30.69788", "29.712438", "29.540501", "29.862627", "29.871033", "29.307488", "29.299856", "29.163054", "28.893345", "30.199352", "29.852911", "29.714425", "29.411667", "29.5848935", "30.334977", "29.333772", "29.871033", "31.951887", "30.681022", "31.167416", "31.405865", "31.080931", "31.022973", "30.936245", "30.306632", "30.010493", "29.299847", "28.8200", "31.405372"],
		districtAndCountyLon: ["107.83394", "106.558941", "106.487634", "106.567583", "106.463447", "106.519108", "106.570124", "106.402277", "106.932780", "106.601921", "106.547442", "108.3869215", "107.346797", "108.779901", "107.085281", "107.738481", "105.865784", "106.265958", "107.106695", "106.675788", "105.843044", "106.062815", "105.729229", "105.596096", "106.203205", "107.339994", "107.767505", "107.738481", "108.665768", "107.815792", "108.400758", "109.579438", "109.884196", "109.468748", "108.698254", "108.04248", "108.117689", "108.164396", "108.7700", "109.574838"]
	}]
}];
var current2 = {
		prov: '',
		city: '',
		country: '',
		provVal: '',
		cityVal: '',
		countryVal: '',
		countryCodeVal: '',
		countryLatVal: '',
		countryLonVal: ''
	};
$(function () {
	var area = {};

	showProv2 = function () {
		$('#addr-wrap').empty();
		/*$('#addr-show02').value = '';*/
		// $('.met2').disabled = true;
		$($('#title-wrap').find('LI')[0]).addClass('titleSel');
		var len = provice.length;
		for (var i = 0; i < len; i++) {
			var provLi = document.createElement('li');
			provLi.innerText = provice[i]['name'];
			provLi.index = i;
			$('#addr-wrap').append(provLi);
		}
	}



	/*选择省份之后显示该省下所有城市*/
	showCity2 = function (index) {
		$('#addr-wrap').empty();;
		current2.prov = index;
		current2.provVal = provice[index].name;
		$($('#title-wrap').find('LI')[0]).removeClass('titleSel');
		$($('#title-wrap').find('LI')[1]).addClass('titleSel');
		var cityLen = provice[index].city.length;
		for (var j = 0; j < cityLen; j++) {
			var cityLi = document.createElement('li');
			cityLi.innerText = provice[index].city[j].name;
			cityLi.index = j;
			$('#addr-wrap').append(cityLi);
		}
	}

	/*选择城市之后显示该城市下所有县区*/
	showCountry2 = function (index) {
		$('#addr-wrap').empty();;
		current2.city = index;
		current2.cityVal = provice[current2.prov].city[index].name;
		$($('#title-wrap').find('LI')[1]).removeClass('titleSel');
		$($('#title-wrap').find('LI')[2]).addClass('titleSel');
		var countryLen = provice[current2.prov].city[index].districtAndCounty.length;
		if (countryLen == 0) {
			$('#addr-show02').text(current2.provVal + '-' + current2.cityVal);
		}
		for (var k = 0; k < countryLen; k++) {
			var cityLi = document.createElement('li');
			cityLi.innerText = provice[current2.prov].city[index].districtAndCounty[k];
			cityLi.setAttribute("id", provice[current2.prov].city[index].districtAndCountyCode[k]);
			cityLi.setAttribute("value", provice[current2.prov].city[index].districtAndCountyLon[k]+","+provice[current2.prov].city[index].districtAndCountyLat[k]);
			cityLi.index = k;
			$('#addr-wrap').append(cityLi);
		}
	}
	return area;
	
});
function initAreaPanel(){
	/*初始化localStorage，默认选中重庆*/
	storage.setItem("areacode","500000");
	$('.locatePanel').remove();
	var h5 = "<div class='locatePanel' style='display: block;'>" +
				"<div id='addr-choice'>" +
					"<ul id='title-wrap' style='padding-top: 10px;'>" +
//						"<li value='0'>省份</li>" +
//						"<li value='1'>城市</li>" +
//						"<li value='2'>县区</li>" +
					"</ul>" +
					"<div id='show-panel'>" +
					"<ul id='addr-wrap'>" +
					"</ul>" +
				"</div>" +
			"</div>" +
			"<button type='button' class='btn met2'>确定</button></div>";
	$('.topHeader').append(h5);
	eventBind();
	/*显示省份*/
	showProv2();
	/*显示重庆城市*/
	showCity2(0);
	showCountry2(0);
}
function eventBind() {

	/*************************需要给动态生成的li绑定点击事件********************** */
	$('#addr-wrap').on('click', function (e) {
		var n;
		var e = e || window.event;
		var target = e.target || e.srcElement;
		if (target && target.nodeName == 'LI') {
			/*先判断当前显示区域显示的是省市区的那部分*/
//			for (var z = 0; z < 3; z++) {
//				if ($($('#title-wrap').find('LI')[z]).hasClass('titleSel'))
//					n = z;
//			}
			/*显示的处理函数*/
//			switch (n) {
//				case 0:
//					showCity2(target.index);
//					break;
//				case 1:
//					showCountry2(target.index);
//					break;
//				case 2:
//					selectCountry(target.index);
//					break;
//				default:
//					showProv2();
//			}
			selectCountry(target.index);
		}
	});

	/*点击确定后将所选地点显示在输入框中*/
	$('.met2').on('click', function () {
		if(current2.provVal==''){

		}else{
			$('#addr-show02').text('');
			if(current2.provVal===current2.cityVal){
				$('#addr-show02').text(current2.provVal + ' ' + current2.countryVal);
			}else{
				if( current2.countryVal == "重庆市") $('#addr-show02').text(current2.provVal);
				else $('#addr-show02').text(current2.provVal + ' ' + current2.countryVal);
			}
			
			$($('#addr-wrap').find('li')[current2.country]).css('background', '#fff');
		}
		/*将所选区域编码存入localStorage中*/
		storage.areacode = current2.countryCodeVal;
		storage.lon = current2.countryLonVal;
		storage.lat = current2.countryLatVal;
		$('.locatePanel').remove();
		
		 if ( storage["areacode"] == "500000" ) {
		 	 document.getElementById('indexIframe').contentWindow.Global.common.map.zoomTo(7);			
		 } else {			
		 }document.getElementById('indexIframe').contentWindow.setCenter(storage["lon"],storage["lat"],storage["areacode"]);

		console.log(storage["areacode"],storage["lon"],storage["lat"]);
	});

	/*分别点击省市区标题的处理函数*/
	$('#title-wrap').click(function (e) {
		var e = e || window.event;
		var target = e.target || e.srcElement;
		if (target && target.nodeName == 'LI') {
			for (var z = 0; z < 3; z++) {
				$($('#title-wrap').find('LI')[z]).removeClass('titleSel');
			}
			$(target).addClass('titleSel');
			if ($(target).val() == '0') {
				showProv2();
			} else if ($(target).val() == '1') {
				showCity2(current2.prov);
			} else {
				showCountry2(current2.city);
			}
		}
	})
}
/*选中具体的县区*/
function selectCountry(index) {
	// $('.met2').disabled = false;
	current2.country = index;
	$($('#addr-wrap').find('LI')[index]).css('background', '#23B7E5');
	current2.countryVal = provice[current2.prov].city[current2.city].districtAndCounty[index];
	current2.countryCodeVal = provice[current2.prov].city[current2.city].districtAndCountyCode[index];
	//选中区县的经度
	current2.countryLonVal = provice[current2.prov].city[current2.city].districtAndCountyLon[index];
	//选中区县的纬度
	current2.countryLatVal = provice[current2.prov].city[current2.city].districtAndCountyLat[index];
}